<?php

namespace App\Models\FuYou;

use App\Models\BaseModel;
use App\Models\Traits\KfUid;
use Illuminate\Database\Eloquent\Model;

class WorkOrderFuyouLogModel extends BaseModel
{
    use KfUid;

    protected $table = 'work_order_fuyou_log';

    protected $fillable = [
        'id',
        'workOrderNo',
        'status',
        'step',
        'content',
        'res',
        'createdByUid',
        'updatedByUid',
        'createdAt',
        'updatedAt',
    ];


    const DEFAULT_STATUS              = 0;//提交到富友
    const DEFAULT_SUC_STATUS          = 1;//成功
    const DEFAULT_FAIL_STATUS         = 2;//富友返回失败
    const DEFAULT_ADMIN_REJECT_STATUS = 3;//管理员驳回

    const STATUS_MAP = [
        self::DEFAULT_STATUS              => "审核中",
        self::DEFAULT_SUC_STATUS          => "成功",
        self::DEFAULT_FAIL_STATUS         => "失败",
        self::DEFAULT_ADMIN_REJECT_STATUS => "管理员驳回"
    ];

    const DEFAULT_STEP                                 = 0; //提交到富友
    const DEFAULT_STEP_BASE_ERR                        = 1; //富友基本信息提交失败
    const DEFAULT_STEP_FILE_UP_ERR                     = 2; //图片上传失败
    const DEFAULT_STEP_CONFIRM_FILE_ERR                = 3; //附件接口请求失败
    const DEFAULT_STEP_WECHAT_CHNL_SUB_MCHID_QUERY_ERR = 4; //微信子商户号查询失败
    const DEFAULT_STEP_ALIPAY_CHNL_SUB_MCHID_QUERY_ERR = 5; //支付宝子商户号查询失败
    const DEFAULT_STEP_WECHAT_CONFIG_GET_ERR           = 6; //微信参数配置接口失败
    const DEFAULT_STEP_BASE_UPDATE_ERR                 = 7; //富友基本信息修改失败
    const DEFAULT_STEP_ACNT_UPDATE_ERR                 = 8; //富友入账信息变更失败
    const DEFAULT_STEP_WX_AUTHORIZE_ERR                = 9; //富友微信二维码认证失败
    const DEFAULT_STEP_ALI_AUTHORIZE_ERR               = 10;//富友支付宝二维码认证失败
    const DEFAULT_STEP_SEND_SECOND_BINDING_EMAIL       = 11;//富友发送一二级绑定邮件
    const DEFAULT_STEP_SEND_PRE_SECRET_APPLY_EMAIL     = 12;//富友发送前置密钥申请
    const DEFAULT_STEP_SUC                             = 99;//富友成功

    const STEP_MAP = [
        self::DEFAULT_STEP                                 => "提交到富友",
        self::DEFAULT_STEP_BASE_ERR                        => "富友基本信息提交失败",
        self::DEFAULT_STEP_FILE_UP_ERR                     => "图片上传失败",
        self::DEFAULT_STEP_CONFIRM_FILE_ERR                => "附件接口请求失败",
        self::DEFAULT_STEP_WECHAT_CHNL_SUB_MCHID_QUERY_ERR => "微信子商户号查询失败",
        self::DEFAULT_STEP_ALIPAY_CHNL_SUB_MCHID_QUERY_ERR => "支付宝子商户号查询失败",
        self::DEFAULT_STEP_WECHAT_CONFIG_GET_ERR           => "微信参数配置接口失败",
        self::DEFAULT_STEP_BASE_UPDATE_ERR                 => "富友基本信息修改失败",
        self::DEFAULT_STEP_ACNT_UPDATE_ERR                 => "富友入账信息变更失败",
        self::DEFAULT_STEP_WX_AUTHORIZE_ERR                => "富友微信二维码认证失败",
        self::DEFAULT_STEP_ALI_AUTHORIZE_ERR               => "富友支付宝二维码认证失败",
        self::DEFAULT_STEP_SEND_SECOND_BINDING_EMAIL       => "富友发送一二级绑定邮件",
        self::DEFAULT_STEP_SEND_PRE_SECRET_APPLY_EMAIL     => "富友发送前置密钥申请",
        self::DEFAULT_STEP_SUC                             => "富友成功",
    ];


    /**
     * 根据id查询详情
     * @param $id
     * @return Model|object|null
     */
    public static function getLogById($id)
    {
        return self::query()->where(['id' => $id])->select('id', 'workOrderNo', 'status')->first();
    }

    /**
     * 新增log
     * @param array $data
     * @return Model
     */
    public static function addLog(array $data): Model
    {
        return self::query()->create($data);
    }

    /**
     * 修改log
     * @param $id
     * @param $data
     * @return int
     */
    public static function setLogById($id, $data): int
    {
        return self::query()->where(['id' => $id])->update($data);
    }


}
